/*
* @Author: Josh
* @Date:   2019-01-27 20:05:59
* @Last Modified by:   Administrator
* @Last Modified time: 2019-04-27 12:01:08
*/
'use strict';
require('page/common/nav/index.js');
require('page/common/header/index.js');
require('./index.css');
var _mall         = require('util/mall.js'),
    _product      = require('service/product-service.js'),
    Pagination    = require('util/pagination/index.js'),
    templateIndex = require('./index.string');

var page = {
    data : {
        listParam : {
            keyword    : _mall.getUrlParam('keyword')     || '',
            categoryId : _mall.getUrlParam('categoryId')  || '',
            orderBy    : _mall.getUrlParam('orderBy')     || 'default',
            pageNum    : _mall.getUrlParam('pageNum')     || 1,
            pageSize   : _mall.getUrlParam('pageSize')    || 15
        }
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadList();
    },
    bindEvent : function(){
        //_this指向page对象
        var _this = this;
        //排序的点击事件
        $('.sort-item').click(function(){
            //$this指向html元素
            var $this = $(this);
            _this.data.listParam.pageNum = 1;
            //点击默认排序
            if($this.data('type') === 'default'){
                //如果已经是active 即已经被选中
                if($this.hasClass('active')){
                    return;
                }
                //若未被选中
                else{
                    $this.addClass('active').siblings('.sort-item')
                        .removeClass('active asc desc');//移除多个class
                    //修改listParam记录的排序顺序
                    _this.data.listParam.orderBy = 'default';
                }
            }
            //点击价格排序
            else if($this.data('type') === 'price'){
                //添加本按钮的选择状态并抹去其他按钮的选中状态
                $this.addClass('active').siblings('.sort-item')
                        .removeClass('active asc desc');
                //升降序的调整
                if(!$this.hasClass('asc')){
                    $this.addClass('asc').removeClass('desc');
                    //修改listParam记录的排序顺序
                     _this.data.listParam.orderBy = 'price_asc';
                }
                else{
                    $this.addClass('desc').removeClass('asc');
                    //修改listParam记录的排序顺序
                     _this.data.listParam.orderBy = 'price_desc';
                }
            }
            //重新加载列表
            _this.loadList();
        });
    },
    //加载list数据
    loadList : function(){
        var listParam = this.data.listParam,
            listHtml  = '',
            _this     = this,
            $pListCon = $('.p-list-con');

        //使得每次初始化列表页都有loading图标
        $pListCon.html('<div class="loading"></div>');

        //删除参数中不必要的字段
        listParam.categoryId ? 
            (delete listParam.keyword) : (delete listParam.categoryId);
        
        //调用接口服务 
        _product.getProductList(listParam, function(res){
            listHtml = _mall.renderHtml(templateIndex, {
                list : res.list
            });

            $pListCon.html(listHtml);
            _this.loadPagination({
                hasPreviousPage : res.hasPreviousPage,
                prePage         : res.prePage,   
                hasNextPage     : res.hasNextPage,
                nextPage        : res.nextPage,   
                pageNum         : res.pageNum,   
                pages           : res.pages      
            });

        }, function(errMsg){
            _mall.errorTips(errMsg);
        });
    },
    //加载分页信息
    loadPagination : function(pageInfo){
        var _this = this;
        //将工具封装成类并判断是否已经引用
        this.pagination ? '' : (this.pagination = new Pagination());
        this.pagination.render($.extend({}, pageInfo, {
            container : $('.pagination'),
            onSelectPage : function(pegeNum){
                _this.data.listParam.pageNum = pegeNum;
                _this.loadList();
            }
        }));
    }
};

$(function(){
    page.init();
})